/*
 * @Description: 
 * @Author: 史迪仔
 * @Date: 2024-11-22 09:03:25
 * @LastEditors: 史迪仔
 * @LastEditTime: 2024-11-25 08:40:48
 */
package com.briup;

import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

/**
 * @program: Maven_demo01
 * @description: xml解析测试类
 * @author: rendc
 * @create: 2024-11-22 09:03
 **/
public class Test {
    public static void main(String[] args) {
        System.out.println("hello,test");
        Test test = new Test();
        String filePath = "src/main/java/com/briup/class.xml";
        test.domParse(filePath);
    }

    // 使用dom解析方式 解析xml文件
    public void domParse(String filePath) {
        // 1. 创建解析类工厂的对象
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        try {
            // 2. 通过工厂创建解析器对象
            DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
            // 3. 解析xml文件
            Document document = documentBuilder.parse(filePath);
            // 4. 获取根元素
            Element root = document.getDocumentElement();
            // System.out.println("root=" + root);
            // 5. 获取root的子节点
            NodeList childNodes = root.getChildNodes();
            // 遍历子节点列表
            for (int i = 0; i < childNodes.getLength(); i++) {
                Node node = childNodes.item(i);
                // System.out.println(node);
                // 判断子节点的类型
                // System.out.println(node.getNodeName());
                // 1 元素 3 文本
                // 只获取元素节点
                if (node.getNodeType() == 1) {
                    // System.out.println(node.getNodeType());
                    // System.out.println(node.getNodeName());
                    NamedNodeMap attributes = node.getAttributes();
                    for (int j = 0; j < attributes.getLength(); j++) {
                        Attr attr = (Attr) attributes.item(j);
                        String attrName = attr.getName();
                        String attrValue = attr.getValue();
                        System.out.println("attrName=" + attrName);
                        System.out.println("attrValue=" + attrValue);
                    }
                    // 获取当前节点的子节点
                    NodeList nodeList = node.getChildNodes();
                    for (int k = 0; k < nodeList.getLength(); k++) {
                        Node item = nodeList.item(k);
                        // 判断子节点是不是元素
                        if (item.getNodeType() == Node.ELEMENT_NODE) {
                            // 节点名
                            String nodeName = item.getNodeName();
                            String nodeText = item.getTextContent();
                            System.out.println("nodeName=" + nodeName);
                            System.out.println("nodeText=" + nodeText);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}